home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-25 | 8.6 KB | 184 lines | [TEXT/MACA] |
- ; (c) Copyright 1990 by University of Massachusetts. All rights reserved.
- ; This software was conceived, designed, and written by Dan Suthers
- ; while supported by the National Science Foundation under grant number
- ; MDR 8751362, and by a fellowship from Apple Computer, Inc., Cupertino,
- ; CA. Partial support was also received from the Office of Naval Research
- ; under a University Research Initiative Grant, contract N00014-86-K-0764.
- ; Mr. Suthers created this software under his own initiative while in an
- ; academic relationship with the University of Massachusetts. The above
- ; copyright notice was a condition placed by University lawyers on approval
- ; of distribution of this software by Apple Computer, and is not meant to
- ; imply that this software was created in an employment or "work for hire"
- ; relationship between the University and Mr. Suthers.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; File: MISC.lisp
- ; Author: Dan Suthers
- ; Created: 15-Oct-86 01:37:00
- ; Modified: 22-Jun-90 02:01:48 (Dan Suthers)
- ; Language: LISP
- ; Package: UTILS
- ;
- ; Description: Miscellaneous utility functions that didn't fit well elsewhere.
- ;
- ; (c) Copyright 1987, by Daniel D. Suthers
- ; Department of Computer and Information Science
- ; University of Massachusetts
- ; Amherst, Massachusetts 01003
- ;
- ; This software was conceived, designed, and written by Dan Suthers
- ; while supported by the National Science Foundation under grant number
- ; MDR 8751362, and by a fellowship from Apple Computer, Inc., Cupertino,
- ; CA. Partial support was also received from the Office of Naval Research
- ; under a University Research Initiative Grant, contract N00014-86-K-0764.
- ; I wish to acknowledge the generous support of Beverly Woolf, who obtained
- ; the above grants and encouraged me to pursue my own research interests in
- ; her lab. This work would not have been possible without the resources and
- ; stimulating environment of the Computer and Information Science department.
- ;
- ; Permission to use, modify, and distribute this software is granted subject
- ; to the following restrictions and understandings:
- ; 1. The file header, including this notice, shall be retained, and may be
- ; extended to include documentation of modifications to the software.
- ; 2. This material is for nonprofit educational and research purposes only.
- ; Users are requested, but not required, to inform Mr. Suthers of any
- ; noteworthy uses of this software.
- ; 3. Mr. Suthers and the University of Massachusetts make no warrantee or
- ; representation that the operation of this software will be error free,
- ; and are under no obligation to provide any services.
- ; 4. Any user of such software agrees to indemnify and hold harmless Mr.
- ; Suthers and the University of Massachusetts from all claims arising
- ; out of the use or misuse of this software, or arising out of any
- ; accident, injury, or damage whatsoever, and from all costs, counsel
- ; fees, and liabilities incurred in or about any such claim, action, or
- ; proceeding brought thereon.
- ; 5. All materials and reports developed as a consequence of the use of
- ; this software shall duly acknowledge such use, in accordance with
- ; the usual standards of acknowledging credit in academic research.
- ;
- ; Status: Working.
- ;
- ; Changes:
- ; Nov-27-88: UNIQUE-SYMBOL fixed to check that INTERN did not find it!
- ; Also, separate counters now kept for each package.
- ; 06-Apr-89: Added INDENT-STRING, deleted SILENT-WARNING.
- ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (in-package :UTILS)
-
- (export '(
- INDENT-STRING
- RESET-PREFIX-COUNTER
- UNIQUE-SYMBOL
- NEXT-UNIQUE-SYMBOL
-
- ))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (let ((*label-counters* nil))
-
- (defun UNIQUE-SYMBOL (prefix &optional (in-package *package*))
- "unique-symbol <prefix> &optional <package> [Function]
- A gentemp variant: returns a unique interned symbol. Difference is it
- always counts from 1 for each new prefix, so you get pretty names."
- (check-type prefix string)
- (check-type in-package package)
- (let* ((package-entry
- (or (assoc in-package *label-counters*)
- (let ((new-entry (cons in-package nil)))
- (push new-entry *label-counters*)
- new-entry)))
- (counter-entry
- (or (assoc prefix (cdr package-entry) :test #'string=)
- (let ((new-entry (cons prefix 0)))
- (push new-entry (cdr package-entry))
- new-entry))))
- (incf (cdr counter-entry))
- (multiple-value-bind
- (interned-symbol already-existed)
- (intern (format nil "~A~A"
- prefix (cdr counter-entry)) in-package)
- (if already-existed
- (unique-symbol prefix in-package)
- interned-symbol))))
-
- (defun NEXT-UNIQUE-SYMBOL (prefix &optional (in-package *package*))
- "next-unique-symbol <prefix> &optional <package> [Function]
- Returns a STRING which would be the name of the next unique-symbol if
- the latter was called with the same arguments."
- (check-type prefix string)
- (check-type in-package package)
- (let* ((package-entry
- (or (assoc in-package *label-counters*)
- (let ((new-entry (cons in-package nil)))
- (push new-entry *label-counters*)
- new-entry)))
- (counter
- (cdr (or (assoc prefix (cdr package-entry) :test #'string=)
- (let ((new-entry (cons prefix 0)))
- (push new-entry (cdr package-entry))
- new-entry)))))
- (incf counter)
- (loop
- (let ((name (format nil "~A~A" prefix counter)))
- (if (find-symbol name in-package)
- (incf counter)
- (return name))))))
-
- (defun RESET-PREFIX-COUNTER (prefix &optional (in-package *package*))
- "reset-prefix-counter <prefix> [Function]
- Resets the UNIQUE-SYMBOL counter for <prefix>. (Deletes the counter from
- an alist -- you may want to do this to shorten the alist as well.)"
- (let ((package-entry (assoc in-package *label-counters*)))
- (if package-entry
- (setf (cdr package-entry)
- (delete prefix (cdr package-entry) :test #'string= :key #'car)))))
-
- ;; For debugging.
- ;; (defun lc () *label-counters*)
-
- )
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmacro SILENT-WARNING (global-var format-string &rest args)
- "silent-warning <global-var> <format-string> &rest args [Macro]
- Used to record trace or warning messages on a global list. Pushes the
- result of applying <format-string> to <args> onto the symbol <global-var>.
- <Args> will be evaluated."
- `(progn (format T "~%Your code is using obsolete SILENT-WARNING macro! Please rewrite.")
- (push (format nil ,format-string ,.args) ,global-var)
- (car ,global-var)))
-
- (defun INDENT-STRING (source-string indentation)
- "indent-string <source-string> <indentation> [Function]
- Returns a new simple-string which is identical to the <source-string>,
- except the beginning is indented by <indentation> spaces, and all
- newlines are followed by <indentation> spaces."
- (declare (string source-string) (fixnum indentation)
- (optimize (safety 1) (space 2) (speed 3)))
- ;; Allocate room for spaces added at beginning and after each newline.
- (let ((target-string-length (+ (* indentation
- (1+ (count #\
- source-string)))
- (length source-string))))
- (declare (fixnum target-string-length))
- (do ((source-index 0 (1+ source-index))
- (target-index indentation (1+ target-index))
- (target-string (make-string target-string-length :initial-element #\ )))
- ((= target-index target-string-length) target-string)
- (declare (fixnum source-index target-index) (simple-string target-string))
- (setf (schar target-string target-index)
- (char source-string source-index))
- (when (char= #\
- (char source-string source-index))
- (dotimes (i indentation)
- (incf target-index)
- (setf (schar target-string target-index) #\ ))))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (provide :MISC)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; EOF
-